package cn.edu.ctbu.yrx.dao;

import cn.edu.ctbu.yrx.entity.Student;
import cn.edu.ctbu.yrx.entity.CourseScore;
import cn.edu.ctbu.yrx.entity.Teacher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TeacherRepository extends JpaRepository<Teacher, Integer> {

    void deleteAllByIdInBatch(Iterable<Integer> ids);
    public List<Teacher> findByTeacherName(String teacherName);

    // 自定义查询方法，使用JOIN FETCH加载用户信息
    @Query("SELECT s FROM teachers s JOIN FETCH s.user")
    List<Teacher> findAllWithUser();

    @Query("SELECT s FROM teachers s JOIN FETCH s.user")
    Page<Teacher> findAllWithUser(Pageable pageable);

    // StudentCourseRepository.java
    @Query("SELECT sc FROM course_score sc WHERE sc.teacher.teacherId = :teacherId")
    Page<CourseScore> findByTeacherId(@Param("teacherId") Integer teacherId, Pageable pageable);

    @Query("SELECT t FROM teachers t WHERE t.user.userId = :userId")
    Teacher findByUserId(@Param("userId") Integer userId);

}